package com.edou.system.controller.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.base.token.TokenValidate;
import com.edou.base.util.DateUtil;
import com.edou.base.util.NumberUtil;
import com.edou.system.entity.SysProgressLog;
import com.edou.system.form.SysProgressLogForm;
import com.edou.system.service.ISysProgressLogService;



/**
 * 操作进度日志
 * @creator
 * @create-time 2017/10/17 15:41:21
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("sysProgressLogAdminController")
@Scope("prototype")
@RequestMapping("/admin/system/progress/log")
public class SysProgressLogAdminController extends BaseController {

	@Resource(name = "sysProgressLogServiceImpl")
	private ISysProgressLogService sysProgressLogService;
	


	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/system/sys_progress_log_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listSysProgressLogByPage.htm", method = RequestMethod.POST)
	public void listSysProgressLogByPage(@ModelAttribute SysProgressLogForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			sysProgressLogService.listSysProgressLogByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","optKey","content"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询操作进度日志时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}
	
	/**
	 * 根据条件查询进度日志
	 * @param model
	 * @param request
	 * @param response
	 * @param session
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/listSysProgressLogByCondition.htm", method = RequestMethod.POST)
	public ModelAndView listSysProgressLogByCondition(@ModelAttribute SysProgressLogForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			List<SysProgressLog> resultList = sysProgressLogService.listSysProgressLogByCondition(model.getCondition());
			
			JSONArray root = new JSONArray();
			for(SysProgressLog item : resultList) {
				JSONObject json = new JSONObject();
				json.put("id", item.getId());
				json.put("content", item.getContent());
				json.put("createDate", DateUtil.formatDate(item.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
				json.put("finishFlag", item.getFinishFlag());
				
				root.add(json);
			}
			
			
			return ajaxJSON(Status.success, "", root);
		}catch(Exception ex){
			String msg = "查询操作进度日志时发生异常："+ex.getMessage();
			logger.error(msg,ex);
			
			return ajaxJSON(Status.error,msg);
		}
	}

	/**
	 * 保存操作进度日志
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute SysProgressLogForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_progress_log_add");
		Map<String,Object> data = view.getModel();
		if(null != model.getItem().getId()){
		SysProgressLog item = sysProgressLogService.getSysProgressLogById(model.getItem().getId());
			data.put("item", item);
		}
		


		return view;
	}

	/**
	 * 保存操作进度日志
	 * @return 
	 */
	@RequestMapping(value = "/saveSysProgressLog.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveSysProgressLog(@ModelAttribute SysProgressLogForm model,HttpServletRequest request,HttpSession session){
		try {
			if(null == model.getItem().getId()){
			sysProgressLogService.add(model.getItem());
			}else{
				sysProgressLogService.update(model.getItem());
			}
			return ajaxJSON(Status.success,"保存成功",true,request);
		} catch (Exception e) {
			String msg = "保存操作进度日志时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试",true,request);
		}
	}
	/**
	 * 删除操作进度日志
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute SysProgressLogForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			sysProgressLogService.delByIds(NumberUtil.convertStringArrayToLongArray(model.getSelIds().split(",")));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除操作进度日志时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}

}